using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entites;

namespace DAL
{
   public class HinhAnhDAL
    {
            private SqlConnection cn = null;
        public HinhAnhDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (HinhAnh ha )
        {
            SqlCommand cm = new SqlCommand("HinhAnh_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaSP",ha.masp);
           cm.Parameters.AddWithValue("@HinhA",ha.hinha);
           cm.Parameters.AddWithValue("@DuongDan",ha.duongdan);
          
            cm.ExecuteNonQuery();
        }
        public void Update(HinhAnh ha)
        {
            SqlCommand cm = new SqlCommand("HinhAnh_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaHinhAnh",ha.mahinhanh);
            cm.Parameters.AddWithValue("@MaSP",ha.masp);
           cm.Parameters.AddWithValue("@HinhA",ha.hinha);
           cm.Parameters.AddWithValue("@DuongDan",ha.duongdan);
          
          
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("HinhAnh_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaHinhAnh", iD);
            cm.ExecuteNonQuery();
        }

        public tbHinhAnhCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("HinhAnh_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbHinhAnhCollection haCollection = new tbHinhAnhCollection();

            while (dr.Read())
            {
                HinhAnh hinhanh = new HinhAnh();
                hinhanh.mahinhanh = int.Parse(dr.GetValue(0).ToString());
                hinhanh.masp = int.Parse(dr.GetValue(1).ToString());
                hinhanh.hinha = dr.GetValue(2).ToString();
                hinhanh.duongdan = dr.GetValue(3).ToString();
                
                haCollection.Add(hinhanh);
             }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return haCollection;
        }
    }
    
    }

